﻿using System;
using Csla.Xaml;
using Accounting.Library;
using System.ComponentModel;
using Utility;
using Accounting.Library.Utility;



namespace csla4Skeleton.UI.SL.NghiepVu.TaiSanCoDinh.TSCD_DieuChinh
{
    public class TSCD_DieuChinh_ViewModel : ViewModel<Accounting.Library.TSCD_DieuChinh>
    {
        #region Event Declare
        public delegate void ChildList_AddedNew_EventHandler(object sender, ChildListAddedNewArgs e);
        public event ChildList_AddedNew_EventHandler ChildList_AddedNew;
        public event EventHandler LoadCompleted;
        public event EventHandler Saved;
        public event EventHandler Canceled;
        public event EventHandler tscdIdChanged;
        public event EventHandler NguyenGiaMoiChanged;
        public event EventHandler ThoiGianSuDungMoiChanged;
        public event EventHandler FinishAddNew;
        #endregion

        #region Refresh Model
        public TSCD_DieuChinh_ViewModel()
        {
            BeginRefresh("NewTSCD_DieuChinh");

        }

        public TSCD_DieuChinh_ViewModel(string mode)
        {
            if (mode.Equals(BusinessConfiguration.Configuration.ChildWindow))
            {
                this.BeginRefresh("NewTSCD_DieuChinh");
            }

        }
        public TSCD_DieuChinh_ViewModel(int Khoa)
        {
            BeginRefresh("GetTSCD_DieuChinh", Khoa);

        }

        public void NewTSCD_DieuChinhViewModel()
        {
            BeginRefresh("NewTSCD_DieuChinh");

        }

        public void GetTSCD_DieuChinhViewModel(int Khoa)
        {
            BeginRefresh("GetTSCD_DieuChinh", Khoa);
        }


        public void SaveAndNew()
        {
            BeginSave();
            NewTSCD_DieuChinhViewModel();
        }
        protected override void OnRefreshed()
        {
            base.OnRefreshed();
            if (Model != null)
            {
                Model.TSCD_DieuChinh_ChiTiets.AddedNew += new EventHandler<Csla.Core.AddedNewEventArgs<TSCD_DieuChinh_ChiTietChild>>(TSCD_DieuChinh_ChiTiets_AddedNew);
              
                Model.TSCDIdChanged += new EventHandler(OnTSCDIdChanged);
                Model.ThoiGianSuDungMoiChanged+= new EventHandler(OnThoiGianSuDungMoiChanged);
                Model.NguyenGiaMoiChanged += new EventHandler(OnNguyenGiaMoiChanged);
                Model.FinishAddNew += new EventHandler(OnFinishAddNew);
                Model.BusyEvent += new EventHandler<IsBusyEventArgs>(DoChangingBusy);
               
                if (LoadCompleted != null)
                {
                    LoadCompleted(this, new EventArgs());
                }
            }
        }
        #endregion

        #region Add TSCD_DieuChinh Details Item
        public void AddTSCD_DieuChinh_ChiTietItem()
        {
            Model.TSCD_DieuChinh_ChiTiets.AddNewItem(Model.Khoa);
        }

       


        void TSCD_DieuChinh_ChiTiets_AddedNew(object sender, Csla.Core.AddedNewEventArgs<TSCD_DieuChinh_ChiTietChild> e)
        {
            OnChildList_AddedNew(sender, new ChildListAddedNewArgs("TSCD_DieuChinh_ChiTiets"));
        }

        


        protected virtual void OnChildList_AddedNew(object sender, ChildListAddedNewArgs e)
        {
            if (ChildList_AddedNew != null)
            {
                //Invokes the delegates.
                ChildList_AddedNew(sender, e);
            }
        }


        public void OnTSCDIdChanged(object sender, EventArgs e)
        {
            if (tscdIdChanged != null)
            {
                tscdIdChanged(sender, e);
            }

        }

        public void OnNguyenGiaMoiChanged(object sender, EventArgs e)
        {
            if (NguyenGiaMoiChanged != null)
            {
                NguyenGiaMoiChanged(sender, e);
            }

        }

        public void OnThoiGianSuDungMoiChanged(object sender, EventArgs e)
        {
            if (ThoiGianSuDungMoiChanged != null)
            {
                ThoiGianSuDungMoiChanged(sender, e);
            }

        }

        public void OnFinishAddNew(object sender, EventArgs e)
        {
            if (FinishAddNew != null)
            {
                FinishAddNew(sender, e);
            }

        }
        public void DoChangingBusy(bool busy)
        {
            IsBusy = busy;
        }
        public void DoChangingBusy(object sender, IsBusyEventArgs e)
        {
            IsBusy = e.Busy;
        }

        protected override void OnSaved()
        {
            base.OnSaved();
            if (Saved != null)
            {
                Saved(this, EventArgs.Empty);
            }
        }

        protected override void DoCancel()
        {
            base.DoCancel();
            if (Canceled != null)
            {
                Canceled(this, EventArgs.Empty);
            }
        }


        #endregion

        #region Delete TSCD_DieuChinh Details Item

        public void DeleteTSCD_DieuChinh_ChiTietItem(int Index)
        {
            Model.TSCD_DieuChinh_ChiTiets.DeleteItem(Index);
        }

      



        #endregion

        #region Nghiep Vu So
        internal void GhiSo()
        {
            this.Model.SaveMode = BusinessConfiguration.Configuration.GhiSoSaveMode;
            BeginSave();
        }

        internal void BoGhi()
        {
            int loaiChungTuId = BusinessConfiguration.Configuration.LoaiChungTuId_TSCD_DieuChinh;
            int chungTuGocId = Model.Khoa;

            this.IsBusy = true;
            Accounting.Library.GhiSo.BoGhiService.BoGhi(loaiChungTuId, chungTuGocId, (o, ex) =>
            {
                if (ex.Error != null)
                    this.Model.ErrorMessage = ex.Error.Message;
                else
                    this.Model.ErrorMessage = ((Accounting.Library.GhiSo.BoGhiService)ex.Object).ErrorMessage;
                BeginRefresh("GetTSCD_DieuChinh", Model.Khoa);
            });
            this.IsBusy = false;
        }
        #endregion
    }
}
